package com.woniu.car.controller;



import com.woniu.car.common.ResponseEntity;


import com.woniu.car.exception.ParamException;
import com.woniu.car.exception.ServiceSException;
import com.woniu.car.param.AddGoodsParam;
import com.woniu.car.param.GroundingParam;
import com.woniu.car.param.ModifyGoodsParam;
import com.woniu.car.service.TGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;


/**
 * <p>
 * 商品基础表 前端控制器
 * </p>
 *
 * @author chestnuts
 * @since 2021-06-21
 */
@Api(tags = "商品模块")
@RestController
@Slf4j
@RequestMapping("/tGoods")
public class TGoodsController {

    @Autowired
    private TGoodsService goodsService;



    @ApiOperation("查询所有商品")
    @PostMapping("queryAllGoods")
    ResponseEntity queryAllGoods(@RequestParam("pageIndex") Integer pageIndex,@RequestParam("pageSize")Long pageSize){

        log.info("商品全查询");
        ResponseEntity responseEntity;


        responseEntity = goodsService.queryAllGoods(pageIndex,pageSize);

        return responseEntity;
    }

    /**
     * 新增一个商品
     * @param addGoodsParam
     * @return
     */
    @ApiOperation("新增商品")
    @PostMapping("addGoods")
    public ResponseEntity addGoods(@Valid AddGoodsParam addGoodsParam, HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException, ServiceSException, ServiceSException, ParamException {
        String header = request.getHeader("token");
        log.info("新增一个商品");
        ResponseEntity responseEntity;

            responseEntity = goodsService.addGoods(addGoodsParam,file,1);

      return responseEntity;
    }

//    @ApiOperation("新增商品")
//    @PostMapping("addGoods")
//    public ResponseEntity addGoods(@Valid AddGoodParam addGoodParam, @RequestParam("file") MultipartFile file){
//        log.info("新增一个商品");
//        ResponseEntity responseEntity;
//        try {
//            responseEntity = goodsService.addGoods(addGoodParam,file,2);
//        } catch (GoodsNullException | IOException e) {
//            return new ResponseEntity().setCode(200).setMsg("该商品已存在,请勿重复添加");
//        }
//        return responseEntity;
//    }

    /**
     *
     * @param modifyGoodsParam
     * @return
     */
    @ApiOperation("修改商品信息,同时修改es信息")
    @PutMapping("putGoods")
    public ResponseEntity putGoods(@RequestBody ModifyGoodsParam modifyGoodsParam) throws ServiceSException {
        ResponseEntity responseEntity;

            responseEntity=goodsService.putGoods(modifyGoodsParam);

        return responseEntity;
    }

    @ApiOperation("删除商品信息")
    @DeleteMapping("delGoods")
    public ResponseEntity delGoods(@RequestParam(value = "goodsId",required = true) Integer goodsId){
        ResponseEntity responseEntity = goodsService.delGoods(goodsId);
        return responseEntity;
    }
    /**
     * 上架商品
     * @param groundingParam
     * @return
     */
    @ApiOperation("上架商品")
    @PostMapping("putOffShelvesGoods")
    public ResponseEntity putGoods(@RequestBody GroundingParam groundingParam) throws ParamException {
        //商品上架 需要参数 商品基础参数 商品规格 商品定价
        ResponseEntity responseEntity=goodsService.groundGoods(groundingParam);
        return responseEntity;
    }

    @ApiOperation("下架商品")
    @DeleteMapping("pullOffShelvesGoods")
    public ResponseEntity pullGoods(@RequestParam(value = "goodsId",required = true) Integer goodsId){
        //商品上架 需要参数 商品基础参数 商品规格 商品定价
        ResponseEntity responseEntity=goodsService.pullGoods(goodsId);
        return responseEntity;
    }

    @ApiOperation("查询上架的商品")
    @GetMapping("searchPutingGoods")
    public ResponseEntity searchPutingGoods(@RequestParam(value = "keyword",required = true) String keyword){
        //商品上架 需要参数 商品基础参数 商品规格 商品定价
        ResponseEntity responseEntity=goodsService.searchPutingGoods(keyword);
        return responseEntity;
    }




    @ApiOperation("根据商品id查询商品详情")
    @GetMapping("getGoods")
    public ResponseEntity getGoods(@RequestParam(value = "goodsId",required = true) Integer goodsId){
        log.info("新增一个商品");

            ResponseEntity responseEntity=goodsService.getGoods(goodsId);

        return responseEntity;
    }

    @ApiOperation("分类查询商品")
    @GetMapping("queryGoods")
    public ResponseEntity queryGoods(@RequestParam(value = "gategoryId",required = true) Integer gategoryId,@RequestParam(value = "gategoryId",required = true)Integer pageIdx){
        log.info("新增一个商品");

        ResponseEntity responseEntity=goodsService.queryGoods(gategoryId,pageIdx);

        return responseEntity;
    }










}

